﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace GTA
{
    public partial class FrmTaskHistoryGeneralClassification : BaseForm
    {
        #region Local Objects
        private List<BusinessEntity.TaskDomain> classifications;
        private BusinessEntity.TaskDomain classificationEdit;
        #endregion

        #region Start Form
        public FrmTaskHistoryGeneralClassification()
        {
            InitializeComponent();
        }
        private void FrmTaskHistoryGeneralClassification_Load(object sender, EventArgs e)
        {
            StartForm();
        }
        private void StartForm()
        {
            try
            {
                LoadClassifications();
            }
            catch (Exception ex)
            {
                HandleException("Inicialização da tela", ex);
            }
        }
        #endregion

        #region Events
        private void cmdSave_Click(object sender, EventArgs e)
        {
            SaveClassification();
        }
        private void cmdEdit_Click(object sender, EventArgs e)
        {
            EditSelectedClassification();
        }
        private void cmdDelete_Click(object sender, EventArgs e)
        {
            DeleteSelectedClassifications();
        }
        private void lstResults_ColumnClick(object sender, ColumnClickEventArgs e)
        {
            SortList(lstResults, e.Column == 0 ? OrdenacaoListView.Tipo.Numerica : OrdenacaoListView.Tipo.Alfabetica, ref columnOrder, ref sortOrder, e.Column);
        }
        private void lstResults_DoubleClick(object sender, EventArgs e)
        {
            EditSelectedClassification();
        }
        private void lstResults_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Delete)
                DeleteSelectedClassifications();
        }
        private void lstResults_Resize(object sender, EventArgs e)
        {
            lstResults.Columns[1].Width = lstResults.Width - lstResults.Columns[0].Width - 23;
        }
        private void editarToolStripMenuItem_Click(object sender, EventArgs e)
        {
            EditSelectedClassification();
        }
        private void excluirToolStripMenuItem_Click(object sender, EventArgs e)
        {
            DeleteSelectedClassifications();
        }
        #endregion

        #region Methods
        private void LoadClassifications()
        {
            try
            {
                BusinessLogic.GTA bl = new BusinessLogic.GTA();
                classifications = bl.ListTaskHistoryGeneralClassifications();
                lstResults.Items.Clear();
                if (classifications != null && classifications.Count > 0)
                {
                    foreach (BusinessEntity.TaskDomain itemResult in classifications)
                    {
                        ListViewItem listItem = new ListViewItem(itemResult.ID.ToString());
                        listItem.SubItems.Add(itemResult.Description);
                        lstResults.Items.Add(listItem);
                    }
                }
            }
            catch (Exception ex)
            {
                HandleException("Carregar a lista de Classificações.", ex);
            }
        }
        private void SaveClassification()
        {
            try
            {
                if (!String.IsNullOrEmpty(txtDescription.Text.Trim()))
                {
                    BusinessEntity.TaskDomain classification = new BusinessEntity.TaskDomain(0, txtDescription.Text);
                    if (classificationEdit != null)
                        classification.ID = classificationEdit.ID;
                    BusinessLogic.GTA bl = new BusinessLogic.GTA();
                    bl.SaveTaskHistoryGeneralClassification(classification);
                    MessageBox.Show("Classificação salva com sucesso!", "Confirmação", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    txtDescription.Text = String.Empty;
                    txtDescription.Focus();
                    classificationEdit = null;
                    LoadClassifications();
                }
                else
                    MessageBox.Show("Informe a descrição!", "Alerta", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            }
            catch (Exception ex)
            {
                HandleException("Salvar os dados da Classificação.", ex);
            }
        }
        private void EditSelectedClassification()
        {
            try
            {
                if (lstResults.SelectedItems.Count == 1)
                {
                    classificationEdit = classifications.Find(it =>
                        it.ID.CompareTo(Convert.ToInt32(lstResults.SelectedItems[0].SubItems[0].Text)) == 0);
                    txtDescription.Text = classificationEdit.Description;
                    txtDescription.Focus();
                }
                else
                    MessageBox.Show("Selecione uma Classificação para editar!", "Alerta", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            }
            catch (Exception ex)
            {
                HandleException("Editar uma Classificação.", ex);
            }
        }
        private void DeleteSelectedClassifications()
        {
            try
            {
                if (lstResults.SelectedItems.Count > 0)
                {
                    if (MessageBox.Show("Deseja realmente excluir a(s) Classificação(ões) selecionada(s)?", "Questão", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
                    {
                        BusinessLogic.GTA bl = new BusinessLogic.GTA();
                        foreach (ListViewItem listItem in lstResults.SelectedItems)
                            bl.DeleteTaskHistoryGeneralClassification(Convert.ToInt32(listItem.SubItems[0].Text));
                        MessageBox.Show("Classificação(ões) excluída(s) com sucesso!", "Confirmação", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        LoadClassifications();
                    }
                }
                else
                    MessageBox.Show("Selecione ao menos uma classificação para excluir!", "Alerta", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            }
            catch (Exception ex)
            {
                HandleException("", ex);
            }
        }
        #endregion
    }
}
